package com.theobserver.theobserver;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;

import java.util.ArrayList;

/**
 * Created by wssxk on 7/7/17.
 * viewpage中的fragment
 */




public class MainPageNewsListFragment extends Fragment {

    Context mContext = null;
    //public String catalog = null;
    public AVObject catalog = null;

    //public int isRefreshing = 0;
    public SwipeRefreshLayout sl = null;
    public NewsListAdapter newslist_adapter = null;
    public int newsPos = 0;     //最后一个显示新闻的坐标
    public RelativeLayout add_more = null;
    public int isAddingMore = 0;

    public void init(Context context)
    {

        mContext = context;
    }

    public void setCatalog(AVObject ca)
    {
        catalog = ca;
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstance)
    {
        View v = View.inflate(mContext, R.layout.news_list_view_layout, null);
        ListView lv = (ListView) v.findViewById(R.id.listview_news);
        //lv.setFooterDividersEnabled(false);
        lv.setDivider(null);
        newslist_adapter = new NewsListAdapter(mContext);
        lv.setAdapter(newslist_adapter);

        final MainPageNewsListFragment thisClass = this;
        /*
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //startActivity(new Intent());
            }
        });
        */

        //下拉刷新
        sl = (SwipeRefreshLayout) v.findViewById(R.id.swiperefreshlayout_news);
        //sl.setRefreshing(false);
        //isRefreshing = 1;
        sl.setRefreshing(true);
        sl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //Toast.makeText(mContext, "Refreshing", Toast.LENGTH_SHORT).show();
                thisClass.refreshNews();
                //sl.setRefreshing(false);
            }
        });

        //拖到底部加载更多
        add_more = v.findViewById(R.id.relativelayout_news_addmore);
        add_more.setVisibility(View.GONE);

        final Context tContext = mContext;
        lv.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int i) {
                ListView mLv = (ListView) absListView;
                if(i == SCROLL_STATE_IDLE)
                {
                    if(mLv.getLastVisiblePosition() == (mLv.getCount()-1))
                    {
                        //Toast.makeText(tContext, "LAST", Toast.LENGTH_SHORT).show();
                        if(mLv.getChildAt(mLv.getLastVisiblePosition()- mLv.getFirstVisiblePosition()).getBottom() <= mLv.getHeight()) {
                            //拖到了底部
                            if(sl.isRefreshing() == false && thisClass.isAddingMore == 0)
                            {
                                add_more.setVisibility(View.VISIBLE);
                                thisClass.isAddingMore = 1;
                                thisClass.getMoreNews();
                            }



                            //Toast.makeText(tContext, "BOTTOM", Toast.LENGTH_SHORT).show();
                            //add_more.setVisibility(View.VISIBLE);
                        }
                    }
                }
            }

            @Override
            public void onScroll(AbsListView absListView, int i, int i1, int i2) {

            }
        });

        //打开新闻详情

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent news_detail_intent = new Intent(mContext, NewsDetailActivity.class);
                //news_detail_intent.putExtras();
                news_detail_intent.putExtra("objectId",
                        ((NewsItemFrameLayout) view.findViewById(R.id.framelayout_news_item)).news.cur_news.getObjectId());
                startActivity(news_detail_intent);
            }
        });

        refreshNews();

        /*
        //获取新闻
        if("推荐".equals(catalog.getString("catalog_name"))) {    //推荐新闻
            News.getAllNews(1, 10, new NewsCallBack() {
                @Override
                public void getNews(ArrayList<News> news, int state, AVException e) {
                    if (thisClass.isRefreshing == 1)
                    {
                        thisClass.isRefreshing = 0;
                        sl.setRefreshing(false);
                    }
                    if (news != null) {
                        newslist_adapter.addItems(news);

                    }
                }
            });
        }
        else        //分类新闻
        {
            News.getCatalogNews(catalog, 1, 10, new NewsCallBack() {
                @Override
                public void getNews(ArrayList<News> news, int state, AVException e) {
                    if (thisClass.isRefreshing == 1)
                    {
                        thisClass.isRefreshing = 0;
                        sl.setRefreshing(false);
                    }
                    if(news != null)
                    {
                        newslist_adapter.addItems(news);

                    }
                }
            });
        }*/



        return v;
    }


    public void refreshNews() {
        //获取新闻

        final MainPageNewsListFragment thisClass = this;
        if ("推荐".equals(catalog.getString("catalog_name"))) {    //推荐新闻
            News.getAllNews(1, 10, new NewsCallBack() {
                @Override
                public void getNews(ArrayList<News> news, int state, AVException e) {
                    if (sl.isRefreshing()) {
                        //thisClass.isRefreshing = 0;
                        sl.setRefreshing(false);
                    }

                    if (news != null) {
                        newslist_adapter.data.clear();
                        newslist_adapter.addItems(news);
                        newslist_adapter.notifyDataSetChanged();
                        thisClass.newsPos = news.size();        //重设最后一个显示新闻的坐标

                    }
                    else
                    {
                        newslist_adapter.data.clear();
                        newslist_adapter.notifyDataSetChanged();
                        thisClass.newsPos = 0;
                    }
                }
            });
        } else        //分类新闻
        {
            News.getCatalogNews(catalog, 1, 10, new NewsCallBack() {
                @Override
                public void getNews(ArrayList<News> news, int state, AVException e) {
                    if (sl.isRefreshing()) {        //结束刷新
                        //thisClass.isRefreshing = 0;
                        sl.setRefreshing(false);
                    }
                    if (news != null) {
                        newslist_adapter.data.clear();          //清空之前的新闻
                        newslist_adapter.addItems(news);
                        newslist_adapter.notifyDataSetChanged();
                        thisClass.newsPos = news.size();

                    }
                    else
                    {
                        newslist_adapter.data.clear();
                        newslist_adapter.notifyDataSetChanged();
                        thisClass.newsPos = 0;
                    }
                }
            });
        }
    }



    public void getMoreNews() {
        //获取新闻

        final MainPageNewsListFragment thisClass = this;
        if ("推荐".equals(catalog.getString("catalog_name"))) {    //推荐新闻
            News.getAllNews(this.newsPos+1, 10, new NewsCallBack() {
                @Override
                public void getNews(ArrayList<News> news, int state, AVException e) {
                    if (sl.isRefreshing()) {
                        //thisClass.isRefreshing = 0;
                        sl.setRefreshing(false);
                    }
                    add_more.setVisibility(View.GONE);
                    thisClass.isAddingMore = 0;         //添加已结束
                    if (news != null) {
                        //newslist_adapter.data.clear();
                        newslist_adapter.addItems(news);
                        newslist_adapter.notifyDataSetChanged();
                        thisClass.newsPos += news.size();        //改变最后一个显示新闻的坐标

                    }

                }
            });
        } else        //分类新闻
        {
            News.getCatalogNews(catalog, this.newsPos + 1, 10, new NewsCallBack() {
                @Override
                public void getNews(ArrayList<News> news, int state, AVException e) {
                    if (sl.isRefreshing()) {        //结束刷新
                        //thisClass.isRefreshing = 0;
                        sl.setRefreshing(false);
                    }
                    thisClass.isAddingMore = 0;
                    add_more.setVisibility(View.GONE);
                    if (news != null) {
                        //newslist_adapter.data.clear();          //清空之前的新闻
                        newslist_adapter.addItems(news);
                        newslist_adapter.notifyDataSetChanged();
                        thisClass.newsPos += news.size();

                    }
                }
            });
        }
    }
}
